package com.itheima;

import org.junit.Test;

import java.util.Comparator;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class TreeMapDemo {
    public static void main(String[] args) {
        test1();
        test2();
    }

    public static void test2() {
        TreeMap<Student,String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });


        treeMap.put(new Student("小明", 18), "北京");
        treeMap.put(new Student("小红", 17), "上海");
        treeMap.put(new Student("小刚", 19), "深圳");
        treeMap.put(new Student("小李", 16), "广州");
        treeMap.put(new Student("小张", 20), "杭州");
    }

    private static void test1() {
        TreeMap<Student,String> treeMap = new TreeMap<>();

        treeMap.put(new Student("小明", 18), "北京");
        treeMap.put(new Student("小红", 17), "上海");
        treeMap.put(new Student("小刚", 19), "深圳");
        treeMap.put(new Student("小李", 16), "广州");
        treeMap.put(new Student("小张", 20), "杭州");

        treeMap.forEach((student, s) -> System.out.println(student.getName() + " " + student.getAge() + " " + s));
    }
}
